package edu.sysu.demo.petrabbit;

import com.google.common.io.BaseEncoding;
import edu.sysu.demo.petrabbit.config.RabbitConfig;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Component;

@Slf4j
@Component
@RabbitListener(queues = RabbitConfig.PROCESS_QUEUE_NAME)
public class Subscriber {

  private BaseEncoding baseEncoding;

  @Autowired
  public Subscriber(BaseEncoding baseEncoding) {
    this.baseEncoding = baseEncoding;
  }

  @RabbitHandler
  public void handle(@Payload byte[] bytes) {
    String message = baseEncoding.encode(bytes);

    log.info("Message {} received at : {}",
        message,
        LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_TIME));
  }
}
